
import OSS from 'ali-oss';


// LTAI5tG5qAzSJjqSKNkmLss5
// TkFrHJxPEB9ghxcL2AU7ehnP3eH667

const private_oss = new OSS({
    region: 'oss-cn-guangzhou',
    accessKeyId: 'LTAI5tG5qAzSJjqSKNkmLss5',
    accessKeySecret: 'TkFrHJxPEB9ghxcL2AU7ehnP3eH667',
    bucket: 'zyrz2024',
});

const  public_oss = new OSS({
    region: 'oss-cn-guangzhou',
    accessKeyId: 'LTAI5tG5qAzSJjqSKNkmLss5',
    accessKeySecret: 'TkFrHJxPEB9ghxcL2AU7ehnP3eH667',
    bucket: 'zyrzpublic',
});


async function getUploadUrl(filePath,type='private') {
    try {
        let url = '';
        if(type === 'private'){
            url = private_oss.signatureUrl(filePath, {
                method: 'put',
                'Content-Type': 'application/x-www-form-urlencoded',
            });
        }
        else{
            url = public_oss.signatureUrl(filePath, {
                method: 'put',
                'Content-Type': 'application/x-www-form-urlencoded',
            });
        }
        return url;
    } catch (error) {
        console.log(error);
        return error;
    }
}

async function getDownUrl(filePath,type='private') {
    try {
        let url = '';
        if(type === 'private'){
            url = private_oss.signatureUrl(filePath);
        }
        else{
            url = public_oss.signatureUrl(filePath);
        }
        return url;
    } catch (error) {
        console.log(error);
        return error;
    }
}


async function uploadFile(ossPath,localPath,type='public') {
    try {
      // 上传文件到OSS，'object'是OSS中的文件名，'localfile'是本地文件的路径。
      let uploadResult=null
      if(type=='public'){
        uploadResult = await public_oss.put(ossPath, localPath);
      }
      else{
        uploadResult= await private_oss.put(ossPath,localPath)
      }
      if(uploadResult.res.status === 200){
          console.log('上传成功');
          //检查url是否为http开头 替换成https
            if(uploadResult.url.indexOf('http://')>-1){
                uploadResult.url=uploadResult.url.replace('http://','https://');
            }
          return{code:1,path:uploadResult.name,url:uploadResult.url};
      }
        else{
            console.log('上传失败');
            return{code:0,msg:JSON.stringify(uploadResult)};
        }
    } catch (error) {
      console.error('阿里云oss上传出错:', error.message);
      return{code:0,msg:error.message};
      // 在此处添加错误处理逻辑。
    }
  }

async function downloadFile(ossPath) {
    try {
      const getResult = await public_oss.get(ossPath);
     // console.log('获取文件成功:', getResult.res);
      return getResult.res.data;
    } catch (error) {
      console.error('阿里云oss下载文件出错:', error.message);
      // 在此处添加错误处理逻辑。
      return null;
    }
  }

// async function getBcckAuth() {
//     const token = await bcckSts.assumeRole('acs:ram::1648980045034289:role/ossstsauth', ``, '3000', 'sessiontest');
//     return token;
// }

export {downloadFile, uploadFile,getUploadUrl,getDownUrl};


// getDownUrl('1.JPEG').then((res) => {
//     console.log(res);
// // });
// downloadFile();